package com.david.system.controller;

import com.david.common.utils.PageResult;
import com.david.system.pojo.DTO.RoleMenuDTO;
import com.david.system.pojo.DTO.UserSearchDTO;
import com.david.system.pojo.Entity.*;
import com.david.system.pojo.VO.DeptVO;
import com.david.system.pojo.VO.MenuVO;
import com.david.system.pojo.VO.UserStatusVO;
import com.david.system.pojo.VO.WarehouseVO;
import com.david.system.pojo.DTO.WarehouseDTO;
import com.david.system.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.david.common.utils.ResultUtils.fail;
import static com.david.common.utils.ResultUtils.success;

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;

//    用户管理

    /**
    * @Description: 所有用户的分页查询
    * @Param: [page, pageSize]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/5/29
    */
    @GetMapping("/user/list")
    public ResponseEntity<Object> userlist(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize) {
        log.info("查询user,{},{}",page,pageSize);
        PageResult result = adminService.userlist(page, pageSize);
        System.out.println(result);
        return success(true,result);
    }

    /**
    * @Description: 用户条件搜索的分页查询
    * @Param: [userSearchVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/5/29
    */
    @PostMapping("/user/searchlist")
    public ResponseEntity<Object> userlistBySearch(@RequestBody UserSearchDTO userSearchDTO) {
        log.info("搜索用户{}", userSearchDTO.getName());
        PageResult result = adminService.userlistBysearch(userSearchDTO);
        System.out.printf(result.toString());
        return success(true, result);
    }

    /**
    * @Description: 部门分级查询
    * @Param: []
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/5/29
    */
    @GetMapping("/dept/casList")
    public ResponseEntity<Object> deptCastList() {
        DeptVO deptVO = adminService.deptCastList();
        List<DeptVO> result = new ArrayList<>();
        result.add(deptVO);
        log.info("部门分级查询{}",result.toString());
        return success(true,result);
    }

    /**
    * @Description: 获取角色列表，用于新增角色时进行配置
    * @Param: []
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/5/29
    */
    @GetMapping("/role/list")
    public ResponseEntity<Object> rolelist() {
        List<RoleEntity> roleEntityList = adminService.getRoleAll();
        return success(true, roleEntityList);
    }

    /**
    * @Description: 批量修改用户状态
    * @Param: [userStatusVO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/5/29
    */
    @PutMapping("/user/statusbatch")
    public ResponseEntity<Object> changeStatusBatch(@RequestBody UserStatusVO userStatusVO) {
        log.info("用户{}批量修改状态为{}",userStatusVO.getUserInfoList(), userStatusVO.getStatusTo());
        List<UserInfo> userInfoList = userStatusVO.getUserInfoList();
        String statusTo = userStatusVO.getStatusTo();
        adminService.changeUsersStatusBatch(userInfoList,statusTo);
        return success(true, "修改成功");
    }
    @PutMapping("/user/status")
    public ResponseEntity<Object> changeStatus(@RequestBody UserEntity user) {
        log.info("修改用户{}状态为{}",user.getId(),user.getStatus());
        adminService.changeUsersStatus(user);
        return success(true, "修改成功");
    }
    /**
    * @Description: 检查用户名是否重复
    * @Param: [username]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/5/31
    */
    @GetMapping("/user/username/{username}")
    public ResponseEntity<Object> checkUsername(@PathVariable("username") String username) {
        log.info("检查用户名是否重复{}",username);
        Integer checked = adminService.checkUsername(username);
        System.out.println(checked);
        return success(true, checked);
    }

    /**
    * @Description: 新增用户
    * @Param: [userInfo]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/5/31
    */
    @PostMapping("/user/add")
    public ResponseEntity<Object> addUser(@RequestBody UserInfo userInfo) {
        log.info("新增用户：{}",userInfo.getUsername());
        if (userInfo.getUsername().isEmpty() || userInfo.getName().isEmpty() || userInfo.getDept().isEmpty() || userInfo.getJob().isEmpty() || userInfo.getRole().isEmpty()) {
            log.info("1");
            return fail(false, "用户信息箱输入错误");
        }
        Integer result = adminService.addUser(userInfo);
        if (result == 1){
            log.info("2");
            return success(true,"用户新增成功");
        } else  {
            log.info("3");
            return fail(false,"用户名冲突");
        }
    }

    /**
    * @Description: 删除用户
    * @Param: [id]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/5/31
    */
    @DeleteMapping("/user/del")
    public ResponseEntity<Object> delUser(@RequestParam Long id) {
        System.out.println(id);
        adminService.delUser(id);
        return success(true, "用户删除成功");
    }

    /**
    * @Description: 修改用户数据
    * @Param: [userInfo]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/3
    */
    @PutMapping("/user/update")
    public ResponseEntity<Object> editUser(@RequestBody UserInfo userInfo) {
        log.info("编辑用户{}",userInfo);
        adminService.editUser(userInfo);
        return success(true, "用户编辑成功");
    }

//    架构管理

    /**
    * @Description: 根据部门ID获取岗位ID
    * @Param: [deptId]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/3
    */
    @GetMapping("/structure/joblist")
    public ResponseEntity<Object> joblistBydeptId(Integer deptId) {
        log.info("查询部门{}的职位列表", deptId);
        List<JobEntity> jobListByDeptId = adminService.getJobListByDeptId(deptId);
        return success(true, jobListByDeptId);
    }

    /**
    * @Description: 修改部门名称
    * @Param: [deptId, deptName]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/5
    */
    @PutMapping("/structure/editDeptName")
    public ResponseEntity<Object> editDeptname(@RequestParam("deptId") Integer deptId, @RequestParam("deptName") String deptName) {
        log.info("修改的部门ID为：{}，修改后的部门名称为：{}",deptId,deptName);
        adminService.editDeptName(deptId,deptName);
        return success(true, "修改成功");
    }

    /**
    * @Description: 新增部门
    * @Param: [parentId, name]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/5
    */
    @PutMapping("/structure/addDept")
    public ResponseEntity<Object> addDept(@RequestParam("parentId") Integer parentId, @RequestParam("deptName") String name) {
        log.info("新增部门{},父ID为：{}",name,parentId);
        Boolean result = adminService.addDept(name, parentId);
        if (result) {
            return success(true,"新增"+name+"成功");
        } else {
            return fail(false, "部门冲突，请修改后添加");
        }
    }

    /**
    * @Description: 按照Id删除部门
    * @Param: [id]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/6
    */
    @DeleteMapping("/structure/delDept/{id}")
    public ResponseEntity<Object> delDept(@PathVariable("id") Integer id){
        log.info("删除部门：{}",id);
        Boolean result = adminService.delDept(id);
        if (result) {
            return success(true, "部门删除成功");
        } else {
            return fail(true, "请先删除子节点");
        }
    }

    /**
    * @Description: 新增岗位
    * @Param: [job]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/6
    */
    @PutMapping("/structure/addJob")
    public ResponseEntity<Object> addJob(@RequestBody JobEntity job) {
        log.info("新增岗位{}",job);
        Boolean result = adminService.addJob(job);
        if (result) {
            return success(true,"新增岗位成功");
        } else {
            return fail(false, "岗位信息有误，请重新添加");
        }
    }

    @DeleteMapping("/structure/delJobs")
    public ResponseEntity<Object> delJobs(@RequestBody List<Integer> ids) {
        log.info(ids.toString());
        adminService.delJobs(ids);
        return success(true, "删除岗位成功");
    }
//    权限管理

    /**
    * @Description: 获取完整权限（menu）列表
    * @Param: []
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/3
    */
    @GetMapping("/permission/menulist")
    public ResponseEntity<Object> menulist() {
        List<MenuVO> list = adminService.getMenuList();
        return success(true, list);
    }

    /**
    * @Description: 获取角色对应的权限menu ID列表
    * @Param: [id]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/4
    */
    @GetMapping("/permission/roleMenuID")
    public ResponseEntity<Object> getRoleMenuIds(Integer id) {
        List<Integer> ids = adminService.getRoleMenuIds(id);
        return success(true, ids);
    }

    /**
    * @Description: 设置角色对应的权限
    * @Param: [roleMenuDTO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/4
    */
    @PostMapping("/permission/setroleMenu")
    public ResponseEntity<Object> setRoleMenuId(@RequestBody RoleMenuDTO roleMenuDTO) {
        Integer roleId = roleMenuDTO.getRoleId();
        List<Integer> ids = roleMenuDTO.getIds();
        log.info("角色ID：{}, 权限IDs：{}", roleId, ids);
        adminService.setRoleMenuId(roleId, ids);
        return success(true, "权限添加成功");
    }

    /**
    * @Description: 删除角色，并同步删除角色对应的权限列表
    * @Param: [id]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/4
    */
    @DeleteMapping("/permission/delrole/{id}")
    public ResponseEntity<Object> delRole(@PathVariable("id") String id) {
        log.info("删除用户{}",id);
        int roleId = Integer.parseInt(id);
        adminService.delRoleById(roleId);
        return success(true, "角色删除成功");
    }

    /**
    * @Description: 新增角色
    * @Param: [role]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/6/5
    */
    @PostMapping("/permission/addRole")
    public ResponseEntity<Object> addRole(@RequestBody RoleEntity role) {
        log.info(role.toString());
        Boolean success = adminService.addRole(role);
        if (success) {
            return success(true, "增加角色成功");
        } else {
            return fail(false, "角色名称或编码冲突，请检查");
        }
    }

    /**
    * @Description: 获取仓库列表
    * @Param: [type]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/7/2
    */
    @GetMapping("/warehouse/whlist")
    public ResponseEntity<Object> getWhlist(@RequestParam("type") String type){
        log.info("获取仓库列表：{}", type);
        List<WarehouseVO> whlist = adminService.getWhlist(type);
        return success(true, whlist);
    }

    /**
    * @Description: 根据部门查询用户
    * @Param: [deptName]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/7/3
    */
    @GetMapping("/warehouse/dept/{deptId}")
    public ResponseEntity<Object> getDeptUserList(@PathVariable Integer deptId) {
        log.info("查询部门{}用户", deptId);
        PageResult userList = adminService.getUserByDeptId(deptId);
        return success(true,userList);
    }

    /**
    * @Description: 查询仓库管理员
    * @Param: [whId]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/7/3
    */
    @GetMapping("/warehouse/wh/{whId}")
    public ResponseEntity<Object> getWhUserList(@PathVariable Integer whId) {
        log.info("查询仓库{}的管理员", whId);
        List<UserInfo> userList = adminService.getWhUserListByWhID(whId);
        return success(true,userList);
    }

    /**
    * @Description: 修改仓库管理员
    * @Param: [whId, userInfos]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/7/4
    */
    @PostMapping("/warehouse/whadmins/{whId}")
    public ResponseEntity<Object> setWhadmins(@PathVariable Integer whId, @RequestBody List<UserInfo> userInfos) {
        log.info("设置{}号仓库，管理员为{}",whId,userInfos);
        adminService.setWhadmins(whId,userInfos);
        return success(true,"管理员修改成功");
    }

    /**
    * @Description: 删除仓库
    * @Param: [whId]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/7/4
    */
    @DeleteMapping("/warehouse/wh")
    public ResponseEntity<Object> delWh(@RequestParam Integer whId) {
        log.info("删除{}号仓库",whId);
        String result = adminService.delWh(whId);
        return success(true, result);
    }

    /**
    * @Description: 获取仓库类型
    * @Param: []
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/7/4
    */
    @GetMapping("/warehouse/whtype")
    public ResponseEntity<Object> getWhType() {
        log.info("获取仓库类型");
        List<WhType> typeList = adminService.getWhType();
        System.out.println(typeList);
        return success(true, typeList);
    }

    /**
    * @Description: 新增或修改仓库信息
    * @Param: [warehouseDTO]
    * @return: org.springframework.http.ResponseEntity<java.lang.Object>
    * @Author: david
    * @Date: 2024/7/4
    */
    @PostMapping("/warehouse/updateWh")
    public ResponseEntity<Object> updateWh(@RequestBody WarehouseDTO warehouseDTO){
        log.info("新增或修改仓库信息{}",warehouseDTO);
        String result = adminService.updateWh(warehouseDTO);
        return success(true,result);
    }

    @PostMapping("/warehouse/type")
    public ResponseEntity<Object> addWhType(@RequestBody WhType whType) {
        log.info("新增类型为：{}",whType);
        adminService.addWhType(whType);
        return success(true,"新增成功");
    }
}
